package com.easybbs.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.easybbs.controller.base.ABaseController;
import com.easybbs.entity.dto.UserMessageCountDto;
import com.easybbs.entity.enums.ArticleStatusEnum;
import com.easybbs.entity.enums.MessageTypeEnum;
import com.easybbs.entity.enums.ResponseCodeEnum;
import com.easybbs.entity.po.UserInfo;
import com.easybbs.entity.po.UserMessage;
import com.easybbs.entity.query.ForumArticleQuery;
import com.easybbs.entity.query.UserIntegralRecordQuery;
import com.easybbs.entity.query.UserMessageQuery;
import com.easybbs.entity.vo.PaginationResultVO;
import com.easybbs.entity.vo.ResponseVO;
import com.easybbs.entity.vo.request.*;
import com.easybbs.entity.vo.response.UserCenterGetUserInfoRspVO;
import com.easybbs.entity.vo.web.ForumArticleVO;
import com.easybbs.exception.BusinessException;
import com.easybbs.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: yudaxian
 * @Description: TODO
 * @DateTime: 2024/10/27 14:26
 **/
@RestController
@Api(tags = "用户中心")
@RequestMapping("/ucenter")
public class UserCenterController extends ABaseController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private AccountService accountService;

    @Resource
    private ForumArticleService forumArticleService;

    @Resource
    private UserIntegralRecordService userIntegralRecordService;

    @Resource
    private UserMessageService userMessageService;

    @ApiOperation(value = "获取用户信息")
    @PostMapping("/getUserInfo")
    public ResponseVO getUserInfo(@RequestBody @Validated UserCenterGetUserInfoReqVO reqVO) {

        UserCenterGetUserInfoRspVO rspVO = userInfoService.getUserInfo(reqVO.getUserId());

        return getSuccessResponseVO(rspVO);
    }

    @SaCheckLogin
    @ApiOperation(value = "获取积分记录")
    @PostMapping("/loadUserIntegralRecord")
    public ResponseVO loadUserIntegralRecord(@RequestBody UserCenterLoadUserIntegralReqVO reqVO) {

        UserInfo userInfo = accountService.getOnlyLoginUserInfo();

        UserIntegralRecordQuery query = new UserIntegralRecordQuery();
        query.setUserId(userInfo.getUserId());
        query.setPageNo(reqVO.getPageNo());
        query.setCreateTimeStart(reqVO.getCreateTimeStart());
        query.setCreateTimeEnd(reqVO.getCreateTimeEnd());
        query.setOrderBy("create_time desc");

        return getSuccessResponseVO(userIntegralRecordService.findListByPage(query));
    }

    @SaCheckLogin
    @ApiOperation(value = "修改用户信息")
    @PostMapping("/updateUserInfo")
    public ResponseVO updateUserInfo(@RequestBody @Validated UserCenterUpdateUserInfoReqVO reqVO) {

        UserInfo userInfo = accountService.getOnlyLoginUserInfo();

        userInfoService.updateUserInfo(userInfo, reqVO);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/loadUserArticle")
    @ApiOperation(value = "获取用户发帖")
    public ResponseVO loadUserArticle(@RequestBody @Validated UserCenterLoadUserArticleReqVO reqVO) {

        ForumArticleQuery articleQuery = new ForumArticleQuery();
        Integer pageNo = reqVO.getPageNo();
        String userId = reqVO.getUserId();
        Integer type = reqVO.getType();
        articleQuery.setOrderBy("post_time desc");
        articleQuery.setPageNo(pageNo);
        if (type == 0) {
            articleQuery.setUserId(userId);
        } else if (type == 1) {
            articleQuery.setCommentUserId(userId);
        } else if (type == 2) {
            articleQuery.setLikeUserId(userId);
        }
        UserInfo userInfo = accountService.getLoginOrNotUserInfo();
        if (userInfo != null) {
            articleQuery.setCurrentUserId(userInfo.getUserId());
        } else {
            articleQuery.setStatus(ArticleStatusEnum.AUDIT.getStatus());
        }
        PaginationResultVO resultVO = forumArticleService.findListByPage(articleQuery);
        return getSuccessResponseVO(convert2PaginationVO(resultVO, ForumArticleVO.class));
    }

    @SaCheckLogin
    @PostMapping("/getMessageCount")
    @ApiOperation(value = "获取用户消息数量")
    public ResponseVO getMessageCount() {

        UserInfo userInfo = accountService.getOnlyLoginUserInfo();
        UserMessageCountDto dto = userInfoService.getMessageCount(userInfo.getUserId());

        return getSuccessResponseVO(dto);
    }

    @SaCheckLogin
    @PostMapping("/loadMessageList")
    @ApiOperation(value = "获取用户消息列表")
    public ResponseVO loadMessageList(@RequestBody @Validated UserCenterLoadMessageListReqVO reqVO) {

        Integer code = reqVO.getCode();
        MessageTypeEnum messageTypeEnum = MessageTypeEnum.getByType(code);
        if (ObjectUtils.isEmpty(messageTypeEnum)) {
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }

        UserInfo userInfo = accountService.getOnlyLoginUserInfo();

        UserMessageQuery query = new UserMessageQuery();
        query.setReceivedUserId(userInfo.getUserId());
        query.setMessageType(reqVO.getCode());
        PaginationResultVO<UserMessage> listByPage = userMessageService.findListByPage(query);

        return getSuccessResponseVO(listByPage);
    }

}
